package com.guavestudios.fcon.commands {
	import com.guavestudios.fcon.api.IControlableConsole;
	import com.guavestudios.fcon.api.INamedCommand;
	import com.guavestudios.fcon.core.PathElement;
	import com.guavestudios.fcon.plugins.ConstantsPlugin;
	
	/**
	* ...
	* @author Default
	*/
	public class ConstantCommand implements INamedCommand{
		
		public function ConstantCommand() {
			
		}
		
		/* INTERFACE com.guavestudios.fcon.api.INamedCommand */
		
		public function execute(args:Array):void{
			var c:IControlableConsole = IControlableConsole(args.shift());
			if (!c.hasPlugin("ConstantsPlugin"))
				return;
				
			var a:String = String(args[0]);
			var cp:ConstantsPlugin = ConstantsPlugin(c.getPlugin("ConstantsPlugin"));
			
			switch (a)
			{
				case "list":
					break;
				case "remove":
					cp.removeConstant(a);
					c.outputText(getCommandName() + ": " + a+"=undefined");
					break;
				default:
					if (args.length > 1)
					{
						var el:Object = args[1];
						if (el is PathElement) el = PathElement(args[1]).getElement();
						cp.setConstant(args[0], el);
						c.outputText(getCommandName() + ": " + formatConstant(a, cp));
						
					}
					else if (a.indexOf("=") > -1)
					{
						var ex:Array = a.split("=");
						if (String(ex[1]).length < 1)
						{
							cp.removeConstant(ex[0]);
							c.outputText(getCommandName() + ": "+ex[0]+"=undefined");
						}
						else
						{
							cp.setConstant(ex[0], ex[1]);
							c.outputText(getCommandName() + ": " + formatConstant(ex[0], cp));
						}
					}
					else
					{
						c.outputText(getCommandName() + ": " + formatConstant(a, cp ));
					}
			}
			
		}
		
		public function getCommandName():String{
			return "constant";
		}
		
		private function formatConstant(id:String,cp:ConstantsPlugin):String
		{
			var obj:Object = cp.getConstant(id);
			if (obj != null)
			{
				return id + "=" + obj.toString();
			}
			return  id + "=null";
		}
		
		
	}
	
}